package cn.edu.swufe.surverpark.struts2.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.Preparable;

import cn.edu.swufe.surverpark.model.Survey;
import cn.edu.swufe.surverpark.model.User;
import cn.edu.swufe.surverpark.struts2.UserAware;
import cn.edu.swufe.surverpark.struts2.service.SurveyService;
import cn.edu.swufe.surverpark.util.ValidateUtil;

/**
 * @author 陈龙 E-mail:529914446@qq.com
 * @version创建时间:2015年4月29日下午11:38:55 类说明
 */
@Controller
@Scope("prototype")
public class SurveyAction extends BaseAction<Survey> implements UserAware,
		ServletContextAware,Preparable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource
	private SurveyService surveyService;

	private List<Survey> mySurveys;

	private String inputPage;
	
	public String mySurveys() {
		this.mySurveys = surveyService.findMySurveys(user);
		return "mySurveyListPage";
	}

	public String newSurvey() {
		this.model = surveyService.newSurvey(user);
		return "designSurveyPage";
	}

	public List<Survey> getMySurveys() {
		return mySurveys;
	}

	public void setMySurveys(List<Survey> mySurveys) {
		this.mySurveys = mySurveys;
	}

	private User user;

	@Override
	public void setUser(User user) {
		this.user = user;
	}

	private Integer sid;

	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}

	public String designSurvey() {
		this.model = surveyService.getSurveyWithChildren(sid);
		return "designSurveyPage";
	}

	public String editSurvey() {
		this.model = surveyService.getSurvey(sid);
		return "editSurveyPage";
	}

	public void prepareUpdateSurvey(){
		inputPage = "/editSurvey.jsp";
	}
	public String updateSurvey() {
		this.sid = model.getId();
		model.setUser(user);
		surveyService.updateSurvey(model);

		return "designSurveyAction";
	}

	public String deleteSurvey() {
		surveyService.deleteSurvey(sid);
		return "findMySurveysAction";
	}

	public String clearAnswers() {
		surveyService.clearAnswers(sid);
		return "findMySurveysAction";

	}

	public String toggleStatus() {
		surveyService.toggleStatus(sid);
		return "findMySurveysAction";
	}

	public String toAddLogoPage() {
		return "addLogoPage";
	}

	private File logoPhoto;
	private String logoPhotoFileName;

	public void prepareDoAddLogo(){
		inputPage = "/addLogo.jsp";
	}
	public String doAddLogo() {
		// 实现上传
		if (ValidateUtil.isValid(logoPhotoFileName)) {
			String dir = sc.getRealPath("/upload");
			String ext = logoPhotoFileName.substring(logoPhotoFileName.lastIndexOf("."));
			long l = System.nanoTime();
			File newFile = new File(dir,l+ext);
			logoPhoto.renameTo(newFile);
			surveyService.updateLogoPhotoPath(sid,"/upload/"+l+ext);
		}
		return "designSurveyAction";
	}

	public File getLogoPhoto() {
		return logoPhoto;
	}

	public void setLogoPhoto(File logoPhoto) {
		this.logoPhoto = logoPhoto;
	}

	public String getLogoPhotoFileName() {
		return logoPhotoFileName;
	}

	public void setLogoPhotoFileName(String logoPhotoFileName) {
		this.logoPhotoFileName = logoPhotoFileName;
	}
	public boolean photoExists(){
		
		String path = model.getLogoPhotoPath();
		if(ValidateUtil.isValid(path)){
			String absPath = sc.getRealPath(path);
			File file = new File(absPath);
			return file.exists();
		}
		return false;
	}

	private ServletContext sc;

	@Override
	public void setServletContext(ServletContext arg0) {
		this.sc = arg0;
	}

	public ServletContext getSc() {
		return sc;
	}

	public void setSc(ServletContext sc) {
		this.sc = sc;
	}

	public String getInputPage() {
		return inputPage;
	}

	public void setInputPage(String inputPage) {
		this.inputPage = inputPage;
	}
	public String analyzeSurvey(){
		this.model = surveyService.getSurveyWithChildren(sid);
		return "analyzeSurveyListPage";
	}

}
